package com.ruoyi.wvp.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.wvp.domain.SurveillanceEvent;
import com.ruoyi.wvp.service.IDutyCountService;
import com.ruoyi.wvp.utils.PermissionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wvp.mapper.SurveillanceSupervisorMapper;
import com.ruoyi.wvp.domain.SurveillanceSupervisor;
import com.ruoyi.wvp.service.ISurveillanceSupervisorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.enums.DeletedEnum;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 督导员审核Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-24
 */
@Service
@Slf4j
public class SurveillanceSupervisorServiceImpl extends ServiceImpl<SurveillanceSupervisorMapper, SurveillanceSupervisor> implements ISurveillanceSupervisorService {
    @Autowired
    private SurveillanceSupervisorMapper surveillanceSupervisorMapper;
    @Autowired
    private IDutyCountService dutyCountService;

    /**
     * 查询督导员审核
     *
     * @param id 督导员审核主键
     * @return 督导员审核
     */
    @Override
    public SurveillanceSupervisor selectSurveillanceSupervisorById(Long id) {
        return surveillanceSupervisorMapper.selectSurveillanceSupervisorById(id);
    }

    /**
     * 查询督导员审核列表
     *
     * @param surveillanceSupervisor 督导员审核
     * @return 督导员审核
     */
    @Override
    public List<SurveillanceSupervisor> selectSurveillanceSupervisorList(SurveillanceSupervisor surveillanceSupervisor) {
        return surveillanceSupervisorMapper.selectSurveillanceSupervisorList(surveillanceSupervisor);
    }

    /**
     * 新增督导员审核
     *
     * @param surveillanceSupervisor 督导员审核
     * @return 结果
     */
    @Override
    public int insertSurveillanceSupervisor(SurveillanceSupervisor surveillanceSupervisor) {
        surveillanceSupervisor.setCreateTime(DateUtils.getNowDate());
        return surveillanceSupervisorMapper.insertSurveillanceSupervisor(surveillanceSupervisor);
    }

    /**
     * 修改督导员审核
     *
     * @param surveillanceSupervisor 督导员审核
     * @return 结果
     */
    @Override
    public int updateSurveillanceSupervisor(SurveillanceSupervisor surveillanceSupervisor) {
        surveillanceSupervisor.setUpdateTime(DateUtils.getNowDate());
        return surveillanceSupervisorMapper.updateSurveillanceSupervisor(surveillanceSupervisor);
    }

    /**
     * 批量删除督导员审核
     *
     * @param ids 需要删除的督导员审核主键
     * @return 结果
     */
    @Override
    public int deleteSurveillanceSupervisorByIds(Long[] ids) {
        return surveillanceSupervisorMapper.deleteSurveillanceSupervisorByIds(ids);
    }

    /**
     * 删除督导员审核信息
     *
     * @param id 督导员审核主键
     * @return 结果
     */
    @Override
    public int deleteSurveillanceSupervisorById(Long id) {
        return surveillanceSupervisorMapper.deleteSurveillanceSupervisorById(id);
    }


    @Override
    public Page<SurveillanceSupervisor> pageList(Page<SurveillanceSupervisor> page, SurveillanceSupervisor surveillanceSupervisor) {
        LambdaQueryWrapper<SurveillanceSupervisor> queryWrapper = getQueryWrapper(surveillanceSupervisor);
        queryWrapper.orderByAsc(SurveillanceSupervisor::getId);
        return page(page, queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public  int onDuty(Long id) {
        SurveillanceSupervisor supervisor = getById(id);
        if(!supervisor.getStatus().equals(0)){
            return 0;
        }
        QueryWrapper<SurveillanceSupervisor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        SurveillanceSupervisor surveillanceSupervisor = new SurveillanceSupervisor();
        surveillanceSupervisor.setUpdateTime(new Date());
        String username = SecurityUtils.getUsername();
        surveillanceSupervisor.setUpdateBy(username);
        surveillanceSupervisor.setStatus(1);
//        surveillanceSupervisor.setDeleted(DeletedEnum.DELETED.getValue());
        boolean update = update(surveillanceSupervisor, queryWrapper);

        dutyCountService.addCount(supervisor.getStreetCode(),supervisor.getEventBeginTime());
        return 1;
    }

    @Override
    public int leaveDuty(Long id) {
        SurveillanceSupervisor supervisor = getById(id);
        if(!supervisor.getStatus().equals(0)){
            return 0;
        }
        QueryWrapper<SurveillanceSupervisor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        SurveillanceSupervisor surveillanceSupervisor = new SurveillanceSupervisor();
        surveillanceSupervisor.setUpdateTime(new Date());
        String username = SecurityUtils.getUsername();
        surveillanceSupervisor.setUpdateBy(username);
        surveillanceSupervisor.setStatus(2);
        boolean update = update(surveillanceSupervisor, queryWrapper);

        return 1;
    }

    @Override
    public void freeSpace() {

        surveillanceSupervisorMapper.freeSpace();
    }

    @Override
    public List<SurveillanceSupervisor> listAll(SurveillanceSupervisor surveillanceSupervisor) {
        return list(getQueryWrapper(surveillanceSupervisor));
    }

    private LambdaQueryWrapper<SurveillanceSupervisor> getQueryWrapper(SurveillanceSupervisor surveillanceSupervisor) {
        LambdaQueryWrapper<SurveillanceSupervisor> queryWrapper = Wrappers.lambdaQuery(SurveillanceSupervisor.class);
        queryWrapper.eq(SurveillanceSupervisor::getDeleted, DeletedEnum.NOMAL.getValue());

        String sql = PermissionUtil.getDeptAddressUnitSql(SecurityUtils.getDeptId());
        queryWrapper.inSql(SurveillanceSupervisor::getCommunityCode,sql);
//        事件编号
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getEventCode())) {
            queryWrapper.eq(SurveillanceSupervisor::getEventCode, surveillanceSupervisor.getEventCode());
        }

        if(Objects.nonNull(surveillanceSupervisor.getEventBeginTime())){
            queryWrapper.eq(SurveillanceSupervisor::getEventBeginTime, surveillanceSupervisor.getEventBeginTime());
        }






//        事件图片
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getPicUrl())) {
            queryWrapper.eq(SurveillanceSupervisor::getPicUrl, surveillanceSupervisor.getPicUrl());
        }

        if("1".equals(surveillanceSupervisor.getHasImg())){
            queryWrapper.isNotNull(SurveillanceSupervisor::getPicUrl);
        }else if ("0".equals(surveillanceSupervisor.getHasImg())){
            queryWrapper.isNull(SurveillanceSupervisor::getPicUrl);
        }




        if (Objects.nonNull(surveillanceSupervisor.getSurveillanceId())) {
            queryWrapper.eq(SurveillanceSupervisor::getSurveillanceId, surveillanceSupervisor.getSurveillanceId());
        }

        //        设备名称
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getDeviceName())) {
            queryWrapper.like(SurveillanceSupervisor::getDeviceName, surveillanceSupervisor.getDeviceName());
        }


//        设备序列号
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getSerialNumber())) {
            queryWrapper.eq(SurveillanceSupervisor::getSerialNumber, surveillanceSupervisor.getSerialNumber());
        }


//        设备编号
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getDeviceNumber())) {
            queryWrapper.eq(SurveillanceSupervisor::getDeviceNumber, surveillanceSupervisor.getDeviceNumber());
        }


//        省份名称
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getProviceName())) {
            queryWrapper.like(SurveillanceSupervisor::getProviceName, surveillanceSupervisor.getProviceName());
        }


//        省份编码
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getProviceCode())) {
            queryWrapper.eq(SurveillanceSupervisor::getProviceCode, surveillanceSupervisor.getProviceCode());
        }


//        市名称
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getCityName())) {
            queryWrapper.like(SurveillanceSupervisor::getCityName, surveillanceSupervisor.getCityName());
        }


//        市编号
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getCityCode())) {
            queryWrapper.eq(SurveillanceSupervisor::getCityCode, surveillanceSupervisor.getCityCode());
        }


//        区名称
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getAreaName())) {
            queryWrapper.like(SurveillanceSupervisor::getAreaName, surveillanceSupervisor.getAreaName());
        }


//        区编码
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getAreaCode())) {
            queryWrapper.eq(SurveillanceSupervisor::getAreaCode, surveillanceSupervisor.getAreaCode());
        }


//        地址
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getAddress())) {
            queryWrapper.eq(SurveillanceSupervisor::getAddress, surveillanceSupervisor.getAddress());
        }


//        社区
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getCommunity())) {
            queryWrapper.eq(SurveillanceSupervisor::getCommunity, surveillanceSupervisor.getCommunity());
        }


//        小区名称
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getResidentialName())) {
            queryWrapper.like(SurveillanceSupervisor::getResidentialName, surveillanceSupervisor.getResidentialName());
        }


//        设备品牌
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getDeviceBrand())) {
            queryWrapper.eq(SurveillanceSupervisor::getDeviceBrand, surveillanceSupervisor.getDeviceBrand());
        }


//        设备型号
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getDeviceModel())) {
            queryWrapper.eq(SurveillanceSupervisor::getDeviceModel, surveillanceSupervisor.getDeviceModel());
        }


//        街道名称
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getStreetName())) {
            queryWrapper.like(SurveillanceSupervisor::getStreetName, surveillanceSupervisor.getStreetName());
        }


//        街道id
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getStreetCode())) {
            queryWrapper.eq(SurveillanceSupervisor::getStreetCode, surveillanceSupervisor.getStreetCode());
        }


//        社区ID
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getCommunityCode())) {
            queryWrapper.eq(SurveillanceSupervisor::getCommunityCode, surveillanceSupervisor.getCommunityCode());
        }


//        小区ID
        if (StringUtils.isNotEmpty(surveillanceSupervisor.getResidentialCode())) {
            queryWrapper.eq(SurveillanceSupervisor::getResidentialCode, surveillanceSupervisor.getResidentialCode());
        }


//        状态 0 待审核 1 确认为督导事件
        if (Objects.nonNull(surveillanceSupervisor.getStatus())) {
            queryWrapper.eq(SurveillanceSupervisor::getStatus, surveillanceSupervisor.getStatus());
        }


        return queryWrapper;
    }
}
